package com.leigang.java.code.batch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: yaner-boot
 * @description: Flow的相关练习
 * @author: leigang
 * @create: 2020-01-01 21:31
 */
@Configuration
@EnableBatchProcessing
public class FlowDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step stepFlowOne(){
        return stepBuilderFactory.get("stepFlowOne").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepFlowOne");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    @Bean
    public Step stepFlowTwo(){
        return stepBuilderFactory.get("stepFlowTwo").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepFlowTwo");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }


    @Bean
    public Step stepFlowThree(){
        return stepBuilderFactory.get("stepFlowThree").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepFlowThree");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    @Bean
    public Flow flowDemoFlow(){
        return new FlowBuilder<Flow>("flowDemoFlow")
                .start(stepFlowOne())
                .next(stepFlowTwo())
                .build();
    }

    @Bean
    public Job flowDemoJob(){
        return jobBuilderFactory.get("flowDemoJob")
                .start(flowDemoFlow())
                .next(stepFlowThree())
                .end()
                .build();
    }
}
